package com.ruoyi.system.utils.huaweiInterfaceUtil;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 华为回传接口（HTTPS）
 * @author: zhengxiong.jiang
 * @createDate: 2025-04-10
 * @version: 1.0
 */
@Component
public class HuaweiInterfaceHttps<T> {

    private T t;

    public HuaweiInterfaceHttps(){

    }

    public HuaweiInterfaceHttps(T t){
        this.t = t;
    }

    //凭证账号
    private static final String X_HW_ID = "APP_Z01YQJ_MES_landai";

    //请求类型
    private static final String HTTP_TYPE = "POST";

    //请求秘钥
    private static final String REQUEST_KEY = "kTLaAzmp6zT9w74dqDW8/A==";

    //签名秘钥
    private static final String SECRET_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDQIyFQKE8XHeD897O8Ww1yfxLRdEVLjCqqpj6pgPV/l6ZdscU781nwp5XEzKNyOW/+we0F5Qy0m355kzL9FMgR9VTDgjsoEFUu99iC4mdwNEKVETtmW+Iqgpj2+78EAVy4sJ93SBMru4EsK4YtMOGPpqbq05KEXbB2My8C33WaxND5BeZlrei3AlL8CFfCuD0LE10l+JwAnF1POU21C7QzC14f8lwz6EfE/6iXF+ya5A6Rlzw5q8QG6d63xZunhrF6EjYQvBbwruEAXpXH8S1Ob3t455OmamVA6pGkES1zh7r1H5lKB9OVaaGii7HZZtjB3NYMZyzzTDnXoUbbb7NVAgMBAAECggEBAL9B6NS9lZ/KZvpNq63DhaHWvzYVP6/iRzq0tRQRWoY6lje2qWUjg1wuwuMqoG7P9OgGz4UBKyGzWQ7igpIozIgkBhX5z95K09vrB9uNrDtg6mY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LdPA5ZM1cIQ5aiKnniH/q21o/3Y9TyYvJsm24GxnE0Cw";

    //加密方式
    private static final String X_HW_SIGN_ALGOS = "hmac-sha256";

    //请求时间
    private static final String DATE = DateUtil.format(new Date(), "yyyyMMdd'T'HHmmss'Z'");

    /**
     * 请求方法
     */
    //父子条码追溯
    public static final String http_medhod_BarcodeRelationSend = "https://apigw-ext.huawei.com/api/mes/htpemstest/rest/BarcodeRelationSend";
    //条码属性回传
    public static final String http_medhod_SubmitBarcodeReleation = "https://apigw-ext.huawei.com/api/mes/htpemstest/rest/SubmitBarcodeReleation";
    //soap方式
    public static final String http_medhod_soap = "https://apigw-ext.huawei.com/api/mes/ictspwtestapiservice/soap/MidLayer";

    /**
     * API请求路径
     */
    public static final String serviceUriBarcodeRelationSend = "/mes/htpemstest/rest/BarcodeRelationSend";
    public static final String serviceUriSubmitBarcodeReleation = "/mes/htpemstest/rest/SubmitBarcodeReleation";
    public static final String serviceUriSoap = "/mes/ictspwtestapiservice/soap/MidLayer";

    /**
     * 请求内容
     */
    private static final String body = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n" +
            "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">   \n" +
            "<soap:Body>     \n" +
            "<Get_Info_Frmbarcode xmlns=\"http://Auto.huawei.com.cn/\">       \n" +
            "<sTaskType>GetEntityRevision</sTaskType>       \n" +
            "<sImport><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" +
            "<GetEntityRevision>\n" +
            "  <Import>\n" +
            "    <EntityName>11</EntityName>\n" +
            "  </Import>\n" +
            "</GetEntityRevision>]]>\n" +
            "</sImport>     \n" +
            "</Get_Info_Frmbarcode>   \n" +
            "</soap:Body> \n" +
            "</soap:Envelope>";


    /**
     * 获取加密后的签名
     *
     * @return
     */
    public static String getSign(String url) {
        String sign = StrUtil.join("|", url, HTTP_TYPE, DATE, X_HW_ID);
        HMac hmac = new HMac(HmacAlgorithm.HmacSHA256, SECRET_KEY.getBytes());
        String xHwSign = hmac.digestHex(sign);
        return xHwSign;
    }

    /**
     * 获取加密后的请求内容
     */
    public static String getXHwBodySign(String data) {
        String bodySign = data + "|" + SECRET_KEY;
        HMac hmac = new HMac(HmacAlgorithm.HmacSHA256, SECRET_KEY.getBytes());
        String xHwBodySign = hmac.digestHex(bodySign);
        return xHwBodySign;
    }

    /**
     * 发送至华为
     */
    public static String sendHuawei(String uri,String http,String data,String date) {
        Map<String, String> header = new HashMap<>();
        header.put("X-HW-ID", X_HW_ID);
        header.put("X-HW-DATE", date);
        header.put("X-HW-SIGN", getSign(uri));
        header.put("X-HW-SIGN-ALGOS", X_HW_SIGN_ALGOS);
        header.put("X-HW-B0DY-SIGN", getXHwBodySign(data));
        HttpRequest httpRequest = HttpRequest.post(http);
        httpRequest.addHeaders(header);
        httpRequest.body(data);
        HttpResponse httpResponse = httpRequest.execute();
        String result = httpResponse.body();
        return result;
    }


    public static void main(String[] args) throws Exception {
        System.out.println("X-HW-SIGN"+"||"+getSign("/mes/ictspwtestapiservice/soap/MidLayer"));
        System.out.println("X-HW-B0DY-SIGN"+"||"+getXHwBodySign(body));
        System.out.println(DATE);
    }

}
